package weaver.interfaces.jyc.action;

import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.text.SimpleDateFormat;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @version 1.0
 * @Title ecology-9
 * @Company 泛微软件
 * @CreateDate 2024/3/18
 * @Description 考勤月结关闭假勤流程
 * @Author AdminZm
 */
public class KQYJCloseWfAction implements Action {

    BaseBean baseBean = new BaseBean();

    @Override
    public String execute(RequestInfo requestInfo) {
        String workflowid = requestInfo.getWorkflowid();
        String requestId = requestInfo.getRequestid();
        int billid = requestInfo.getRequestManager().getBillid();
        String formtable = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog("KQYJCloseWfAction start:" + workflowid + "、" + requestId);
        try {
            RecordSet rs = new RecordSet();
            RecordSet rs1 = new RecordSet();
            // 获取最新操作日期
            rs.execute("SELECT TOP 1 sdyf FROM uf_kqgzglb WHERE sfsd = 0 ORDER BY sdyf DESC");
            String lastDate = null;
            if (rs.next()) {
                lastDate = rs.getString("sdyf");
            }
            baseBean.writeLog("lastDate:" + lastDate);
            if (StringUtils.isEmpty(lastDate)) {
                return Action.SUCCESS;
            }

            // 定义一个DateTimeFormatter对象，用于解析和格式化年月字符串
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");
            // 将字符串解析为YearMonth对象
            YearMonth specifiedYearMonth = YearMonth.parse(lastDate, formatter);
            // 获取下一个月
            YearMonth nextMonth = specifiedYearMonth.plusMonths(1);

            String nextMonthStr = nextMonth.format(formatter);
            nextMonthStr = nextMonthStr + "-01";

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            // 根据流程配置字段获取日期值
            rs.executeQuery("SELECT * FROM uf_ybgblcglb WHERE lcid = ?", workflowid);
            if (rs.next()) {
                String rqzdly = rs.getString("rqzdly");
                String rqzdm = rs.getString("rqzdm");
                String djmxb = rs.getString("djmxb");

                if (StringUtils.isEmpty(rqzdly) || StringUtils.isEmpty(rqzdm)) {
                    baseBean.writeLog("KQYJCloseWfAction rqzdly or rqzdm set error.");
                    return Action.SUCCESS;
                }

                if (StringUtils.equals(rqzdly, "1") && StringUtils.isEmpty(djmxb)) {
                    baseBean.writeLog("KQYJCloseWfAction djmxb set error.");
                    return Action.SUCCESS;
                }
                String sql = "";
                if (StringUtils.equals(rqzdly, "0")) {
                    sql = "SELECT * FROM " + formtable + " WHERE id =" + billid;
                }
                if (StringUtils.equals(rqzdly, "1")) {
                    sql = "SELECT * FROM " + formtable + "_dt" + djmxb + " WHERE mainid =" + billid;
                }

                baseBean.writeLog("KQYJCloseWfAction sql:" + sql);
                rs1.execute(sql);
                while (rs1.next()) {
                    String qsrq = rs1.getString(rqzdm);
                    if (StringUtils.isEmpty(qsrq)) {
                        continue;
                    }
                    Date date1 = sdf.parse(nextMonthStr);
                    Date date2 = sdf.parse(qsrq);
                    if (date2.compareTo(date1) < 0) {
                        requestInfo.getRequestManager().setMessagecontent("当前申请日期所在考勤周期已关闭，无法操作！");
                        return Action.FAILURE_AND_CONTINUE;
                    }
                }
            }
            baseBean.writeLog("KQYJCloseWfAction end.");
        } catch (Exception e) {
            baseBean.writeLog("KQYJCloseWfAction error:" + e.getMessage());
        }
        return Action.SUCCESS;
    }
}
